package com.example.li_han.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class RndImage {
	public static String random(){//产生五位随机数
		String str="";
		int num=100000+(int)(Math.random()*100000);
		str=str+num;
		str=str.substring(1,str.length());
		return str;
	}
	private static Color getRandomColor(){//随机颜色
		Color col=null;
		Random rnd=new Random();
		col=new Color(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255));
		return col;
	}
	//生成图片
	public static void imageOut(String num,OutputStream out,int width,int height) throws IOException{
		BufferedImage rndImg=null; //������ͼ��
		rndImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		Graphics2D g=(Graphics2D)rndImg.getGraphics(); //��άͼ����
		g.setColor(Color.WHITE);
		g.fillRect(0,0,width,height);//添加填充的矩形
		Font mFont=new Font("Tahoma",Font.BOLD,height*3/4);//设置字体
		g.setFont(mFont);
		String str1[]=new String[5];
		for (int i = 0; i < str1.length; i++) {
			str1[i]=""+num.charAt(i);
			g.setColor(getRandomColor());
			g.drawString(str1[i],20*i+20,height*4/5);
		}
		g.dispose();
		ImageIO.write(rndImg,"jpg",out); //���ڴ�ͼ��д���������
	}

}
